package com.user.controller;

import com.microservice.entity.Role;
import com.microservice.entity.User;
import com.user.feignClient.UserFeignClient;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.*;

import com.user.service.UserService;

import io.swagger.annotations.ApiOperation;

import java.util.List;


/**
 * 用户控制类
 * 
 * @author Administrator
 *
 */
@RestController
public class UserController {

	@Autowired
	private UserService userService;

	@Autowired
	private UserFeignClient userFeignClient;

	@Autowired
	private LoadBalancerClient balancerClient;


	@ApiOperation(value="通过ID获取用户",notes ="")
	@GetMapping(value = "/get/{id}")
	public User getUserById(@PathVariable("id") int id) {
		return userService.selectByPrimaryKey(id);
	}


	@ApiOperation(value = "登录")
	@GetMapping(value = "/login")
	public int getUserById(String name,String loginPass) {
		return userService.login(name, loginPass);
	}


	@ApiOperation(value = "通过用户ID获取角色列表")
	@ApiParam(value = "userId",name = "用户ID",required = true)
	@GetMapping(value = "/list/byUserId/{userId}")
	public List<Role> getRolesByUserId(@PathVariable("userId") Integer userId){

		ServiceInstance si = balancerClient.choose("microservice-role");
		System.out.println("Feign请求地址："+si.getHost()+":"+si.getPort());

		return userFeignClient.getRolesByUserId(userId);
	};


	@ApiOperation(value = "测试事务")
	@PostMapping(value = "/save")
	public int saveUser(){
		return userService.saveUser();
	}
}
